package com.qmai.util.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chuchengyi
 */
public class PageResult<T> implements Serializable {
    private static final long serialVersionUID = 3703154210703924121L;
    /**
     * 功能：是否成功
     */
    private boolean status;

    /**
     * 功能：错误编码
     */
    private long code;

	/**
	 * 功能：错误消息
	 */
	private String message;

    /**
     * 功能：数据对象
     */
    private List<T> data;


    /**
     * 功能：数据总量
     */
    private int total;


    public PageResult() {
        this.status = false;
    }

    /**
     * 功能：通过设置成功标识
     *
     * @param status
     */
    public PageResult(boolean status) {
        this.status = status;

    }

    /**
     * 功能：通过成功标志和数据对象来构造结果
     *
     * @param status
     * @param data
     */

    public PageResult(boolean status, List<T> data) {
        this.status = status;
        this.data = data;
    }


    /**
     * 功能：通过成功标识返回的数据结果 和异常信息来构造对象
     *
     * @param status
     * @param data
     * @param code
     * @param message
     */
    public PageResult(boolean status, List<T> data, long code, String message) {
        this.status = status;
        this.data = data;
        this.code = code;
        this.message = message;
    }


    /**
     * 功能：设置错误信息
     *
     * @param status
     * @param data
     * @param info
     */
    public PageResult(boolean status, List<T> data, ExceptionInfo info) {
        this.status = status;
        this.data = data;
        this.code = info.getErrorCode();
        this.message = info.getErrorMessage();
    }

	public PageResult(boolean status, long code, String message, List<T> data, int total) {
		this.status = status;
		this.code = code;
		this.message = message;
		this.data = data;
		this.total = total;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public long getCode() {
		return code;
	}

	public void setCode(long code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	/**
     * 功能：直接定义异常
     *
     * @param info
     */
    public void setException(ExceptionInfo info) {
        this.code = info.getErrorCode();
        this.message = info.getErrorMessage();
    }



    /**
     * 功能：设置异常信息
     *
     * @return
     */
    public ExceptionInfo exceptionInfo() {
        return new ExceptionInfo(this.getCode(), this.getMessage());
    }


	/**
	 * 功能：返回success PageResult
	 *
	 * @param data
	 * @param total
	 * @param <R>
	 * @return
	 */
	public static <R>  PageResult<R> successData(List<R> data, int total) {
		return new PageResult<>(true, CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage(), data, total);
    }

	/**
	 * 能：返回空的 PageResult
	 * @param <R>
	 * @return
	 */
	public static <R>  PageResult<R> successEmptyData() {
		return new PageResult<>(true, CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage(), new ArrayList<>(), 0);
	}
	/**
	 * 功能：返回failure PageResult
	 *
	 * @param exceptionInfo
	 * @param <R>
	 * @return
	 */
	public static <R>  PageResult<R> failureData(ExceptionInfo exceptionInfo) {
		return new PageResult<>(false, exceptionInfo.getErrorCode(), exceptionInfo.getErrorMessage(), null, 0);
	}


	@Override
	public String toString() {
		return "PageResult{" +
				"status=" + status +
				", code=" + code +
				", message='" + message + '\'' +
				", data=" + data +
				", total=" + total +
				'}';
	}
}
